package com.wing.cabinet.controller.app;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.CutterLifeCycle;
import com.wing.cabinet.model.request.CutterLifeCycleForm;
import com.wing.cabinet.model.response.CutterLifeCycleVO;
import com.wing.cabinet.service.CutterLifeCycleService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-刀具生命周期管理", tags = "刀具生命周期管理")
@RestController("appCutterLifeCycleController")
@RequestMapping("cabinet/app/cutterLifeCycle")
public class CutterLifeCycleController {

    @Autowired
    private CutterLifeCycleService cutterLifeCycleService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有刀具生命周期", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "packLabelCode", value="刀具条码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuCode", value="物料编号", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "skuName", value="物料名称", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "specCode", value="规格码", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "usherInId", value="领新人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "status", value="状态", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "curOperatorId", value="当前操作人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
            ,@ApiImplicitParam(name = "isCheck", value="是否盘验", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "inspectorId", value="盘验人ID", defaultValue = "", example = "", dataType = "Long", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CutterLifeCycle> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "id") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String packLabelCode
            ,String skuCode
            ,String skuName
            ,String specCode
            ,Long usherInId
            ,CutterLifeCycle.Status status
            ,Long curOperatorId
            ,Integer isCheck
            ,Long inspectorId
            ,String query
    ){

        Page<CutterLifeCycle> pg = new Page<>(page, limit);
        QueryWrapper<CutterLifeCycle> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        // 搜索
        if(StringUtils.isNotBlank(packLabelCode)){
            queryWrapper.like("pack_label_code", packLabelCode);
        }
        if(StringUtils.isNotBlank(skuCode)){
            queryWrapper.like("sku_code", skuCode);
        }
        if(StringUtils.isNotBlank(skuName)){
            queryWrapper.like("sku_name", skuName);
        }
        if(StringUtils.isNotBlank(specCode)){
            queryWrapper.like("spec_code", specCode);
        }
        if(null != usherInId){
            queryWrapper.eq("usher_in_id", usherInId);
        }
        if(null != status){
            queryWrapper.eq("status", status.getValue());
        }
        if(null != curOperatorId){
            queryWrapper.eq("cur_operator_id", curOperatorId);
        }
        if(null != isCheck){
            queryWrapper.eq("is_check", isCheck);
        }
        if(null != inspectorId){
            queryWrapper.eq("inspector_id", inspectorId);
        }
        if (StringUtils.isNotBlank(query)){
            queryWrapper.like("sku_code", query).or().like("sku_name", query).or().like("spec_code",query);
        }

        IPage<CutterLifeCycle> list = cutterLifeCycleService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long cutterLifeCycleId) {
        CutterLifeCycleVO cutterLifeCycleVO = cutterLifeCycleService.get4appById(cutterLifeCycleId);
        if (cutterLifeCycleVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cutterLifeCycleVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CutterLifeCycleForm cutterLifeCycleForm) {
        if (cutterLifeCycleService.add(cutterLifeCycleForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
